package com.uzai.console.web.vph;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.device.DeviceQuery;
import com.uzai.console.dto.jd.jdorder.JdOrderIdDto;
import com.uzai.console.dto.tb.tborder.TbOrderUnbindDtoQuery;
import com.uzai.console.dto.user.UserQuery;
import com.uzai.console.dto.vph.vphorder.*;
import com.uzai.console.dto.vph.vphtoken.VphTokenQuery;
import com.uzai.console.entity.VphToken;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.VphTokenMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.device.DeviceTypeService;
import com.uzai.console.service.user.UserService;
import com.uzai.console.service.wechatprivate.DeviceWechatInfoService;
import com.uzai.console.service.order.OrderReStatusService;
import com.uzai.console.service.order.OrderSourceService;
import com.uzai.console.service.order.OrderStatusService;
import com.uzai.console.service.vph.VphOrderService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.device.DeviceInfoVo;
import com.uzai.console.vo.tb.tborder.TbOrderReportVo;
import com.uzai.console.vo.user.UserInfoVo;
import com.uzai.console.vo.order.OrderReStatusVo;
import com.uzai.console.vo.order.OrderSourceVo;
import com.uzai.console.vo.order.OrderStatusVo;
import com.uzai.console.vo.vph.vphorder.VphOrderInfoVo;
import com.uzai.console.vo.vph.vphorder.VphOrderReportVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "唯品会订单",tags = "唯品会|唯品会订单")
@ApiSort(12)
@RestController
public class VphOrderController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(VphOrderController.class);

    @Autowired
    private VphOrderService vphOrderService;
    @Autowired
    private OrderStatusService orderStatusService;
    @Autowired
    private OrderReStatusService orderReStatusService;
    @Autowired
    private OrderSourceService orderSourceService;
    @Autowired
    private VphTokenMapper vphTokenMapper;
    @Autowired
    private DeviceWechatInfoService deviceWechatInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "查询唯品会订单列表", notes = "查询唯品会订单列表", response = VphOrderInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/vphOrder/findVphOrderList")
    public Object findVphOrderList(@RequestBody VphOrderQuery vphOrderQuery){
        return vphOrderService.findVphOrderList(vphOrderQuery);
    }

    @ApiOperation(value = "查询唯品会订单详情", notes = "查询唯品会订单详情", response = VphOrderInfoVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/vphOrder/findVphOrderInfo")
    public Object findVphOrderInfo(@RequestBody VphOrderIdDto vphOrderIdDto){
        return vphOrderService.findVphOrderInfo(vphOrderIdDto);
    }

    @ApiOperation(value = "查询唯品会订单统计信息(总收入、买家总金额、总利润、未绑定用户利润、未收货返利金额）", notes = "查询唯品会订单统计信息(总收入、买家总金额、总利润、未绑定用户利润、未收货返利金额）", response = VphOrderReportVo.class)
    @ApiOperationSupport(order = 3)
    @PostMapping("/vphOrder/findVphOrderReport")
    public Object findVphOrderReport(@RequestBody VphOrderQuery vphOrderQuery){
        return vphOrderService.findVphOrderRepot(vphOrderQuery);
    }

    @SystemLogType(model = SystemModelEnum.VphOrder, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "订单删除", notes = "订单删除")
    @ApiOperationSupport(order = 4)
    @PostMapping("/vphOrder/delVphOrder")
    public Object delVphOrder(@RequestBody VphOrderDelDto vphOrderDelDto){
        return vphOrderService.delVphOrder(vphOrderDelDto);
    }

    @SystemLogType(model = SystemModelEnum.VphOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "确认收货", notes = "确认收货")
    @ApiOperationSupport(order = 5)
    @PostMapping("/vphOrder/confirmReceive")
    public Object confirmReceive(@RequestBody VphOrderIdDto vphOrderIdDto){
        return vphOrderService.confirmReceive(vphOrderIdDto);
    }

    @SystemLogType(model = SystemModelEnum.VphOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "手动维权", notes = "手动维权")
    @ApiOperationSupport(order = 6)
    @PostMapping("/vphOrder/confirmRefund")
    public Object confirmRefund(@RequestBody VphOrderIdDto vphOrderIdDto){
        return vphOrderService.confirmRefund(vphOrderIdDto);
    }

    @SystemLogType(model = SystemModelEnum.VphOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "订单冻结", notes = "订单冻结")
    @ApiOperationSupport(order = 7)
    @PostMapping("/vphOrder/confirmFrozen")
    public Object confirmFrozen(@RequestBody VphOrderIdDto vphOrderIdDto){
        return vphOrderService.confirmFrozen(vphOrderIdDto);
    }

    @SystemLogType(model = SystemModelEnum.VphOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "订单解冻", notes = "订单解冻")
    @ApiOperationSupport(order = 8)
    @PostMapping("/vphOrder/confirmThaw")
    public Object confirmThaw(@RequestBody VphOrderIdDto vphOrderIdDto){
        return vphOrderService.confirmThaw(vphOrderIdDto);
    }

    @SystemLogType(model = SystemModelEnum.VphOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "解绑用户", notes = "解绑用户")
    @ApiOperationSupport(order = 9)
    @PostMapping("/vphOrder/unbindUser")
    public Object unBindUser(@RequestBody VphOrderUnbindUserDto vphOrderUnbindUserDto){
        return vphOrderService.unbindUser(vphOrderUnbindUserDto);
    }

    @SystemLogType(model = SystemModelEnum.VphOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "绑定用户", notes = "绑定用户")
    @ApiOperationSupport(order = 10)
    @PostMapping("/vphOrder/bindUser")
    public Object bindUser(@RequestBody VphOrderBindUserDto vphOrderBindUserDto){
        return vphOrderService.bindUser(vphOrderBindUserDto);
    }

    @SystemLogType(model = SystemModelEnum.VphOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改解冻时间", notes = "修改解冻时间")
    @ApiOperationSupport(order = 11)
    @PostMapping("/vphOrder/updateThawTime")
    public Object updateThawTime(@RequestBody VphOrderThawTimeDto vphOrderThawTimeDto){
        return vphOrderService.updateThawTime(vphOrderThawTimeDto);
    }

    @ApiOperation(value = "查询联盟账号接口", notes = "查询联盟账号接口", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 12)
    @PostMapping("/vphOrder/findAlimmList")
    public Object findAlimmList(){
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        VphTokenQuery vphTokenQuery = new VphTokenQuery();
        vphTokenQuery.setMerId(getLoginInfo().getMerId());
        List<VphToken> vphTokenList = vphTokenMapper.findByList(vphTokenQuery);
        if(vphTokenList != null){
            int now = DateUtil.getNowTime();
            for (VphToken vphToken : vphTokenList){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(vphToken.getId()));
                selectKeyVo.setValue(vphToken.getOpenId());
                if(vphToken.getExpiresTime().intValue() <= now){
                    selectKeyVo.setExptimeFlag(1); //已到期
                }else{
                    selectKeyVo.setExptimeFlag(0); //未到期
                }
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }

    @SystemLogType(model = SystemModelEnum.VphOrder, type = OperationTypeEnum.DOWNLOAD)
    @ApiOperation(value = "手动下载唯品会订单", notes = "手动下载唯品会订单")
    @ApiOperationSupport(order = 13)
    @PostMapping("/vphOrder/downloadOrder")
    public Object downloadOrder(@RequestBody VphOrderDownLoadDto vphOrderDownLoadDto){
        vphOrderService.downloadOrder(vphOrderDownLoadDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.VphOrder, type = OperationTypeEnum.DOWNLOAD)
    @ApiOperation(value = "订单刷新", notes = "订单刷新")
    @ApiOperationSupport(order = 14)
    @PostMapping("/vphOrder/refreshOrder")
    public Object refreshOrder(@RequestBody VphOrderIdDto vphOrderIdDto){
        vphOrderService.refreshOrder(vphOrderIdDto);
        return true;
    }

    @ApiOperation(value = "查询唯品会订单状态列表", notes = "查询唯品会订单状态列表", response = OrderStatusVo.class)
    @ApiOperationSupport(order = 17)
    @PostMapping("/vphOrder/getOrderStatusList")
    public Object getOrderStatusList() {
        return orderStatusService.findVphOrderStatusList();
    }

    @ApiOperation(value = "查询唯品会订单返利状态列表", notes = "查询唯品会订单返利状态列表", response = OrderReStatusVo.class)
    @ApiOperationSupport(order = 18)
    @PostMapping("/vphOrder/getOrderReStatusList")
    public Object getOrderReStatusList() {
        return orderReStatusService.findOrderReStatusList();
    }

    @ApiOperation(value = "查询唯品会订单来源列表", notes = "查询唯品会订单订单来源列表", response = OrderSourceVo.class)
    @ApiOperationSupport(order = 19)
    @PostMapping("/vphOrder/getOrderSourceList")
    public Object getOrderSourceList() {
        return orderSourceService.findOrderSourceList();
    }

    @ApiOperation(value = "查询机器人下拉列表", notes = "查询机器人下拉列表", response = DeviceInfoVo.class)
    @ApiOperationSupport(order = 20)
    @PostMapping("/vphOrder/findDeviceWechatInfoSelectList")
    public Object findDeciceWechatInfoList() {
        DeviceQuery deviceQuery = new DeviceQuery();
        deviceQuery.setMerId(getLoginInfo().getMerId());
        return deviceService.findDeviceList(deviceQuery);
    }

    @ApiOperation(value = "分页查询会员信息列表", notes = "分页查询会员信息列表", response = UserInfoVo.class)
    @ApiOperationSupport(order = 22)
    @PostMapping("/vphOrder/findUserPage")
    public Object findUserPage(@RequestBody UserQuery userQuery) {
        return userService.findUserPage(userQuery);
    }

    @ApiOperation(value = "查询设备类型下拉列表", notes = "查询设备类型下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 23)
    @PostMapping("/vphOrder/findDeiviceTypeListSelect")
    public Object findDeiviceTypeListSelect() {
        return deviceTypeService.findDeviceTypeListSelect();
    }

    
    @ApiOperation(value = "查询所有未绑定用户订单", notes = "查询所有未绑定用户订单", response = VphOrderInfoVo.class)
    @ApiOperationSupport(order = 15)
    @PostMapping("/vphOrder/findUnbindUserList")
    public Object findUnbindUserList(@RequestBody VphOrderUnbindDtoQuery vphOrderUnbindDtoQuery){
        return vphOrderService.findUnbindUserList(vphOrderUnbindDtoQuery);
    }

    @ApiOperation(value = "查询所有未绑定用户统计信息(总收入、买家总金额、总利润）", notes = "查询所有未绑定用户订单统计信息(总收入、买家总金额、总利润）", response = VphOrderReportVo.class)
    @ApiOperationSupport(order = 23)
    @PostMapping("/vphOrder/findUnbindUserReport")
    public Object findUnbindUserReport(@RequestBody VphOrderUnbindDtoQuery vphOrderUnbindDtoQuery){
        return vphOrderService.findUnbindUserReport(vphOrderUnbindDtoQuery);
    }

    @SystemLogType(model = SystemModelEnum.VphOrder, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除所有未绑定用户订单", notes = "删除所有未绑定用户订单", response = Boolean.class)
    @ApiOperationSupport(order = 16)
    @PostMapping("/vphOrder/deleteUnbindUserList")
    public Object deleteUnbindUserList(@RequestBody VphOrderDeleteUnbindDto vphOrderDeleteUnbindDto){
        return vphOrderService.deleteUnbindUserList(vphOrderDeleteUnbindDto);
    }

    @SystemLogType(model = SystemModelEnum.VphOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "更新7天前未收货订单", notes = "更新7天前未收货订单", response = Boolean.class)
    @ApiOperationSupport(order = 17)
    @PostMapping("/vphOrder/updateVphOrderUnReceived")
    public Object updateVphOrderUnReceived(){
        vphOrderService.updateVphOrderUnReceivedList(getLoginInfo().getMerId());
        return true;
    }

    @SystemLogType(model = SystemModelEnum.VphOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "追回返利", notes = "追回返利")
    @ApiOperationSupport(order = 18)
    @PostMapping("/vphOrder/rebateRefound")
    public Object rebateRefound(@RequestBody VphOrderIdDto vphOrderIdDto){
        return vphOrderService.rebateRefound(vphOrderIdDto);
    }
}
